/**
 * Created by Administrator on 2017/4/19.
 */
$(document).ready(function () {
    $('#switcher').hover(function () {
        $(this).addClass('hover');
    }, function () {
        $(this).removeClass('hover');
    });

    var toggleSwitcher = function (e) {
        if (!$(e.target).is('button')) {
            $('#switcher button').toggleClass('hidden');
        }
    };

    $('#switcher').on('click', toggleSwitcher);
    $('#switcher').click();

    var setBodyClass = function (className) {
        $('body').removeClass().addClass(className);
        $('#switcher button').removeClass('selected');
        $('#switcher-' + className).addClass('selected');
        $('#switcher').off('click', toggleSwitcher);
        if (className == 'default') {
            $('#switcher').on('click', toggleSwitcher);
        }
    };
    $('#switcher').addClass('selected');
    var triggers = {
        D: 'default',
        N: 'narrow',
        L: 'large'
    };
    $('#switcher').click(function (e) {
        if ($(e.target).is('button')) {
            var bodyClass = e.target.id.split('-')[1];
            setBodyClass(bodyClass);
        }
    });

    $(document).keyup(function (e) {
        var key = String.fromCharCode(e.keyCode);
        if (key in triggers) {
            setBodyClass(triggers[key]);
        }
    });
});